package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 关联查询
 */

public class JDBCDemo5 {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection()) {
            Statement satatement = connection.createStatement();
            String sql = "select t.name,t.salary,t.title,c.name,c.floor " +
                    "from teacher t , class c where t.id = c.teacher_id " +
                    "and t.name = '王克晶' ";
            System.out.println(sql);
            ResultSet resultSet = satatement.executeQuery(sql);
            while (resultSet.next()){
                //支持字段索引,从1开始
                String name = resultSet.getString(1);
                double salary = resultSet.getDouble(2);
                String title = resultSet.getString(3);
                String className = resultSet.getString(4);
                int floor = resultSet.getInt(5);
//                String name = resultSet.getString("t.name");
//                double salary = resultSet.getDouble("t.salary");
//                String title = resultSet.getString("t.title");
//                String className = resultSet.getString("c.name");
//                int floor = resultSet.getInt("c.floor");
                System.out.println(name+" "+salary+" "+title+" "+className+" "+floor);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
